﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Configuration;
using System.Web.Configuration;
using Dare.Utilities.Data;
using System.ComponentModel;
using System.IO;
using System.Web;

namespace Stone.Components
{
    public class AppSettings
    {
        const string CFG_KEY_DB_TYPE = "DB_TYPE";
        const string CFG_KEY_DB_LOCATION = "DB_LOCATION";
        const string CFG_KEY_DB_NAME = "DB_NAME";
        const string CFG_KEY_DB_PORT = "DB_PORT";
        const string CFG_KEY_DB_USER = "DB_USER";
        const string CFG_KEY_DB_PASSWORD = "DB_PASSWORD";
        const string CFG_KEY_APP_LOG_PATH = "APP_LOG_PATH";
        const string CFG_KEY_APP_NAME = "APP_NAME";
        const string CFG_KEY_APP_RESPATH = "RES_PATH";

        Configuration config;
        KeyValueConfigurationCollection settings;
        [Browsable(false)]
        public Configuration Config
        {
            get { return config; }
            set
            {
                if (value == null) throw new ArgumentNullException();
                config = value;
                settings = value.AppSettings.Settings;
            }
        }

        public AppSettings()
        {
            if (System.Web.HttpContext.Current == null)
            {
                Config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            }
            else
            {
                Config = WebConfigurationManager.OpenWebConfiguration("~");
            }
        }

        public AppSettings(Configuration config)
        {
            this.Config = config;
        }

        public string GetSettingValue(string key)
        {
            return settings[key] == null ? null : settings[key].Value;
        }

        public void SetSettingValue(string key, string val)
        {
            if (settings[key] == null)
            {
                settings.Add(key, val);
            }
            else
            {
                settings[key].Value = val;
            }
        }

        public void SaveChanges()
        {
            config.Save(ConfigurationSaveMode.Minimal);
        }

        #region 设置属性

        public string DbType
        {
            get { return GetSettingValue(CFG_KEY_DB_TYPE); }
            set { SetSettingValue(CFG_KEY_DB_TYPE, value); }
        }

        public string DbLocation
        {
            get { return GetSettingValue(CFG_KEY_DB_LOCATION); }
            set { SetSettingValue(CFG_KEY_DB_LOCATION, value); }
        }

        public ushort DbPort
        {
            get { return DbConvert.ToNullableUInt16(GetSettingValue(CFG_KEY_DB_PORT)) ?? 0; }
            set { SetSettingValue(CFG_KEY_DB_PORT, value.ToString()); }
        }

        public string DbName
        {
            get { return GetSettingValue(CFG_KEY_DB_NAME); }
            set { SetSettingValue(CFG_KEY_DB_NAME, value); }
        }

        public string DbUser
        {
            get { return GetSettingValue(CFG_KEY_DB_USER); }
            set { SetSettingValue(CFG_KEY_DB_USER, value); }
        }

        public string DbPassword
        {
            get { return GetSettingValue(CFG_KEY_DB_PASSWORD); }
            set { SetSettingValue(CFG_KEY_DB_PASSWORD, value); }
        }

        public string AppLogPath
        {
            get { return GetSettingValue(CFG_KEY_APP_LOG_PATH); }
            set { SetSettingValue(CFG_KEY_APP_LOG_PATH, value); }
        }

        public string AppName
        {
            get { return GetSettingValue(CFG_KEY_APP_NAME); }
            set { SetSettingValue(CFG_KEY_APP_NAME, value); }
        }

        public string ResPath
        {
            get { return GetSettingValue(CFG_KEY_APP_RESPATH); }
            set { SetSettingValue(CFG_KEY_APP_RESPATH, value); }
        }

        #endregion

        public bool IsDebug = true;


        public string GetResourcePath()
        {
            string resPath = ResPath;
            if (String.IsNullOrEmpty(resPath)) resPath = "resources";

            if (!Path.IsPathRooted(resPath))
            {
                if (HttpContext.Current != null)
                {
                    return HttpContext.Current.Server.MapPath(resPath);
                }
                else
                {
                    return Path.GetFullPath(resPath);
                }
            }

            return resPath;
        }

    }
}
